//
//  MoreViewController.m
//  Kfangchan
//
//  Created by liran on 14/11/15.
//  Copyright (c) 2014年 Beijing Renjuren Network Technology Co., Ltd. All rights reserved.
//

#import "MoreViewController.h"
#import "FeedbackViewController.h"
#import "SDImageCache.h"

@interface MoreViewController () {
    UIButton *countButton;
}

@property (strong, nonatomic) IBOutlet UIView *messageView;
@property (strong, nonatomic) IBOutlet UILabel *messageLabel;

@property (strong, nonatomic) IBOutlet UIView *guideView;

@property (strong, nonatomic) IBOutlet UIView *feedbackView;

@property (strong, nonatomic) IBOutlet UIView *cacheView;

@property (strong, nonatomic) IBOutlet UILabel *cacheLabel;

@end

@implementation MoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.titleView = [[Theam currentTheam] navigationTitleViewWithTitle:@"更多"];
    self.navigationItem.leftBarButtonItem = [[Theam currentTheam] navigationBarLeftButtonItemWithImage:[UIImage imageNamed:@"building_top_left.png"] Target:nil Selector:nil];
    
    self.messageView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.messageView.layer.shadowOffset = CGSizeMake(0, 1);
    self.messageView.layer.shadowOpacity = 0.1;
    self.messageView.layer.shadowRadius = 1;
    
    self.cacheView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.cacheView.layer.shadowOffset = CGSizeMake(0, 1);
    self.cacheView.layer.shadowOpacity = 0.1;
    self.cacheView.layer.shadowRadius = 1;
    
    UIImageView *guideLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"partition.png"]];
    guideLine.frame = CGRectMake(0, 46, 320, 0.5);
    [self.guideView addSubview:guideLine];
    
    UIImageView *feedbackLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"partition.png"]];
    feedbackLine.frame = CGRectMake(0, 46, 320, 0.5);
    [self.feedbackView addSubview:feedbackLine];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    __weak MoreViewController *weak_self = self;
    MKNetworkOperation *operation = [[BaseEngine sharedEngine]RunRequest:nil path:KFC_MESSAGE_COUNT completionHandler:^(id responseObject) {
        int noReadCount = [[responseObject valueForKey:@"noReadCount"] intValue];
        if (noReadCount <= 0) {
            if (countButton) {
                [countButton removeFromSuperview];
            }
            return;
        }
        if (countButton == nil) {
            countButton = [UIButton buttonWithType:UIButtonTypeCustom];
            countButton.userInteractionEnabled = NO;
        }
        if (noReadCount > 9) {
            [countButton setBackgroundImage:[UIImage imageNamed:@"more_message_count2.png"] forState:UIControlStateNormal];
            countButton.size = CGSizeMake(20, 16);
            countButton.contentEdgeInsets = UIEdgeInsetsMake(0, 1, 1, 0);
        } else {
            [countButton setBackgroundImage:[UIImage imageNamed:@"more_message_count1.png"] forState:UIControlStateNormal];
            countButton.size = CGSizeMake(19, 20);
            countButton.contentEdgeInsets = UIEdgeInsetsMake(1, 1, 0, 0);
        }
        [countButton setTitle:[NSString stringWithFormat:@"%i", noReadCount] forState:UIControlStateNormal];
        [countButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        countButton.titleLabel.font = [UIFont boldSystemFontOfSize:11];
        countButton.center = CGPointMake(weak_self.messageLabel.center.x + 40, weak_self.messageLabel.center.y);
        [weak_self.messageView addSubview:countButton];
    } errorHandler:^(NSError *error) {
        showCustomAlertMessage(@"网路错误");
    } finishHandler:^(id responseObject) {
    }];
    [self.opHandlers addObject:operation];
    
    NSUInteger size = [self capacity];
    float capacity = size / 1024.0 / 1024.0;
    self.cacheLabel.text = [NSString stringWithFormat:@"%.1fMB", capacity];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark 消息盒子
- (IBAction)messageBox:(id)sender {
    MsgListViewController *mvc = [[MsgListViewController alloc] init];
     mvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:mvc animated:YES];
}

#pragma mark -
#pragma mark 如何推荐客户赚取佣金
- (IBAction)commission:(id)sender {
    
    CmsGuideViewController *cms = [[CmsGuideViewController alloc] init];
     cms.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:cms animated:YES];

}

#pragma mark -
#pragma mark 意见反馈
- (IBAction)feedback:(id)sender {
    FeedbackViewController *fvc = [[FeedbackViewController alloc] init];
    fvc.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:fvc animated:YES];
}

#pragma mark -
#pragma mark 清除缓存
- (NSUInteger)capacity {
    NSUInteger capacity = [[SDImageCache sharedImageCache] getSize];
    return capacity;
}

- (IBAction)clearCache {
    [[SDImageCache sharedImageCache] clearDisk];
    //[[SDImageCache sharedImageCache] clearMemory];
    showCustomAlertMessage(@"清理完毕");
    self.cacheLabel.text = @"0.0MB";
    
}

@end
